package com.my.blog.config;

import com.my.blog.interceptor.AccessInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/***
 *
 * @Auther: MuYun
 * @Date: 2021/9/14 15:38
 * @Description：
 *
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Resource
    private AccessInterceptor accessInterceptor;

    /**
     * 通过url获取静态资源，绝对路径，应该是通过url映射到实际磁盘位置
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        //获取工程当前的根目录
        String s = System.getProperty("user.dir");
        System.out.println("工程当前的根目录："+s);
        registry.addResourceHandler("/upload/**").addResourceLocations("file:"+s+"/upload/");
    }

    /**
     * 注册拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(accessInterceptor);
    }
}
